Welcome to “Packaging.” After watching this video, you will be able
to: <click>Differentiate between Python module,
package, and library <click>Create a Python package <click>Verify a Python package <click>And use a Python package Modules, packages, and libraries are frequently
used terms in Python. Let’s look at each of the terms in detail. A Python module is a .py file containing Python
definitions, statements, functions, and classes. You can import a module to other scripts and
notebooks. For example, consider a module named module.py
that has two functions. The first function is def square open round
bracket number close round bracket colon, return number asterisk asterisk two, which
squares the input and returns the result as an output. The second function is def doubler open round
bracket number close round bracket colon, return number asterisk two, which doubles
the input and returns the result as an output. If the function is in the same directory,
you can import it and use the functions in that module. Let’s consider using the square function
with print command. print open round bracket double quotes four
carat symbol two double quotes comma square open round bracket four close round bracket
close round bracket. The output is displayed as four carat symbol
two equals sixteen. Similarly for the print doubler function with
value 4, the output is two asterisk four equals eight. A package is a collection of python modules
into a dictionary with a __init__.py file, which distinguishes it from just a dictionary
of python scripts. The example shows the package myproject in
parent_directory with two modules, module 1 dot pie and module 2 dot pie It also contains the underscore underscore
init underscore underscore dot pie file. When you import a module or a package, the
corresponding object created by Python is always of type module. Note that the distinction between module and
package is only at the file system level. A library is a collection of packages or it
can be a single package. Examples include numpy, pytorch, and Pandas. Note that the terms package and library are
often used interchangeably. Therefore, numpy, pytorch, and Pandas are
also referred to as packages. Let’s look at the steps to create a Python
package. Consider there are two modules, module1.py
has two functions, square and doubler. Module2.py has one function, mean. To make myproject folder into a package, you
must have __init__.py as a file in the myproject folder. The contents of the __init__.py file must
be from dot import module one and from dot import module two. Typical steps to create a package include: First, create a folder with the package name. Then, create an empty __init__.py file. Create the required modules. Finally, in the __init__.py file, add code
to reference the modules needed in the package. After creating the package, you need to verify
the package. To verify the package: First, open a bash terminal. Make sure the directory is the same as the
folder where your package is located. Open the python interpreter by running the
command python in the shell. Using the python prompt type import followed
by the project name, for example import myproject. If the command runs without errors, it is
an indication that the package is successfully loaded. The general structure for testing your package
is the package name dot module name dot function name followed by the parameters without the
curly brackets but keeping the round brackets. For example, using myproject, myproject dot
basic dot square open round bracket two close round bracket, the function will return the
value 4. After creating the package, you can use it
in other scripts if the package folder is in the same directory. In this case, you have the file Test.Py in
the “parent directory”, you can import the functions in the package. For example, using the Python code from my project dot module one import square
comma doubler from my project dot module two import mean print open round bracket double quotes four
carat symbol two equals double quotes comma square round bracket four close round bracket
close round bracket. print open round bracket double quotes two
asterisk four equals double quotes comma doubler open round bracket four close round bracket
close round bracket. print open round bracket double quotes open
round bracket 2 plus 1 plus three close round bracket divided by three equals double quotes
comma mean open round bracket open square bracket two comma one comma three close square
bracket close round bracket close round bracket. You can then run the functions and check
if you get the correct results. In this video, you learned that: A Python module is a .py file containing Python
definitions, statements, functions, and classes. A package is a collection of python Python
modules into a dictionary with a __init__.py file. A library is a collection of packages or it
can be a single package. To create a package: Create a folder with the package name. Create an empty __init__.py file. Create the required modules. In the __init__.py file, add code to reference
the modules needed in the package. You can verify the package via the bash terminal. After creating the package, you can use it
in other scripts if the package folder is in the same directory.